package com.litongjava.ai.server.padddle.ocr.service.impl;

import com.benjaminwan.ocrlibrary.OcrResult;
import com.litongjava.ai.server.padddle.ocr.common.FileUtil;
import com.litongjava.ai.server.padddle.ocr.service.OcrService;
import io.github.mymonstercat.Model;
import io.github.mymonstercat.ocr.InferenceEngine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;

@Service
public class OcrServiceImpl implements OcrService {


    @Value("${file.upload.path}")
    private String uploadPath;


    @Override
    public String recognizeByUrl(String url) {
        // TODO: 在这里实现通过URL识别图片文字的逻辑
        return "URL图片识别结果将在这里实现";
    }

    @Override
    public String recognizeByFile(MultipartFile file) {
        try {
            InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
            File filePath = FileUtil.getFile(file, uploadPath);
            OcrResult ocrResult = engine.runOcr(filePath.getAbsolutePath());
            return ocrResult.getStrRes();
        } catch (IOException e) {
            throw new RuntimeException("文件处理失败", e);
        }
    }

    @Override
    public String recognizeByResource(URL resource) {
        // TODO: 在这里实现通过资源识别图片文字的逻辑
        return "资源图片识别结果将在这里实现";
    }
}